/**
 * 
 */
package br.srv.full.virtual.entities.menu;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import br.srv.full.virtual.entities.auth.Role;

/**
 * @author Delfino
 * 
 */
@Entity
@DiscriminatorValue(value = "B")
@DiscriminatorColumn(name = "menutype", discriminatorType = DiscriminatorType.CHAR)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "menu", uniqueConstraints = { @UniqueConstraint(columnNames = { "menutype", "name" }) })
public abstract class MenuBase implements Serializable {

	@Id
	@Column(name = "idmenu")
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer id;

	// Sugere a ordem do menu.
	@OrderBy
	@Column(name = "menuorder", nullable = false)
	@Basic(optional = false)
	private Integer priority = 99999999;

	@Column(unique = true, nullable = false)
	@Basic(optional = false)
	private String name;

	@Basic(optional = false)
	private String label;

	@Basic(optional = true)
	private String description;

	@Basic(optional = true)
	private String menuTopHint;

	@Column(unique = true)
	private String menuHelpLink;

	@ManyToOne(targetEntity = MenuGroup.class, optional = true)
	@JoinColumn(name = "idgroup", referencedColumnName = "idmenu", nullable = true)
	private MenuGroup group;

	/**
	 * Papeis que podem usar este menu!
	 * 
	 * @see #noLogged
	 */
	@ManyToMany(targetEntity = Role.class)
	@JoinTable(name = "menuroles", joinColumns = @JoinColumn(name = "idmenu", referencedColumnName = "idmenu"), inverseJoinColumns = @JoinColumn(name = "rolename", referencedColumnName = "rolename"))
	private List<Role> roles;

	/**
	 * permite que usuários náo logados possam ver! mas isto náo remove a
	 * necessidade de ter ou náo um determinado papel (role)
	 * 
	 * @see #roles
	 */
	@Column(name = "noLogged")
	private Boolean onlyForNoLogged;

	public String getLabel() {
		return label;
	}

	public void setLabel(String p_label) {
		label = p_label;
	}

	public String getName() {
		return name;
	}

	public void setName(String p_name) {
		name = p_name;
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param p_id
	 *            the id to set
	 */
	public void setId(Integer p_id) {
		id = p_id;
	}

	private Boolean disabled = false;

	/**
	 * @return
	 */
	public Boolean getDisabled() {
		return disabled;
	}

	/**
	 * @return the disabled
	 */
	public Boolean isDisabled() {
		if (disabled == null)
			disabled = false;
		return disabled;
	}

	/**
	 * @param p_disabled
	 *            the disabled to set
	 */
	public void setDisabled(Boolean p_disabled) {
		disabled = p_disabled;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return this.getClass().getSimpleName() + ": " + getName();
	}

	/**
	 * @return the priority
	 */
	public Integer getPriority() {
		return priority;
	}

	/**
	 * @param p_order
	 *            the priority to set
	 */
	public void setPriority(Integer p_order) {
		priority = p_order;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.srv.full.virtual.entities.base.MenuBase#getGroup()
	 */
	public MenuGroup getGroup() {
		return group;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.srv.full.virtual.entities.base.MenuBase#setGroup(br.srv.full.virtual.entities.base.MenuGroup)
	 */
	public void setGroup(MenuGroup p_group) {
		group = p_group;
	}

	/**
	 * @return the roles
	 */
	public List<Role> getRoles() {
		return roles;
	}

	/**
	 * @param p_roles
	 *            the roles to set
	 */
	public void setRoles(List<Role> p_roles) {
		roles = p_roles;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param p_description
	 *            the description to set
	 */
	public void setDescription(String p_description) {
		description = p_description;
	}

	/**
	 * @return the menuTopHint
	 */
	public String getMenuTopHint() {
		return menuTopHint;
	}

	/**
	 * @param p_menuTopHint
	 *            the menuTopHint to set
	 */
	public void setMenuTopHint(String p_menuTopHint) {
		menuTopHint = p_menuTopHint;
	}

	/**
	 * @return the menuHelpLink
	 */
	public String getMenuHelpLink() {
		return menuHelpLink;
	}

	/**
	 * @param p_menuHelpLink
	 *            the menuHelpLink to set
	 */
	public void setMenuHelpLink(String p_menuHelpLink) {
		menuHelpLink = p_menuHelpLink;
	}

	/**
	 * @return the onlyForNoLogged
	 */
	public Boolean getOnlyForNoLogged() {
		if (onlyForNoLogged == null)
			onlyForNoLogged = false;
		return onlyForNoLogged;
	}

	/**
	 * @param p_onlyForNoLogged
	 *            the onlyForNoLogged to set
	 */
	public void setOnlyForNoLogged(Boolean p_onlyForNoLogged) {
		onlyForNoLogged = p_onlyForNoLogged;
	}

}
